package com.chenglai.slideshow.controler;

import com.chenglai.commonutils.common.Result;
import com.chenglai.commonutils.common.StatusCode;
import com.chenglai.slideshow.pojo.Slideshow;
import com.chenglai.slideshow.service.SlideshowService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.StringReader;
import java.util.List;

/**
 * 类描述：
 *
 * @author Jiang
 * @date 2023/11/14 13:30
 */
@CrossOrigin
@RestController
@RequestMapping("/slideshow")
@Api(value = "/slideshow", tags = {"轮播图服务"})
public class SlideshowController {

    @Resource
    private SlideshowService slideshowService;

    @PostMapping("/getSlideshow")
    @ApiOperation(value = "获取轮播图")
    public Result getSlideshow(@ApiParam(name = "shopId", value = "门店Id") @RequestParam("shopId")Integer shopId,
                               @ApiParam(name = "flag", value = "flag 0非置顶 1置顶") @RequestParam("flag")Integer flag,
                               @ApiParam(name = "type", value = "type 轮播图位置 1首页") @RequestParam("type")Integer type){
        try {
            List<Slideshow> slideshowList = slideshowService.getSlideshow(shopId,flag,type);
            return new Result(true, StatusCode.OK,"添加成功",slideshowList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @PostMapping("/addSlideshow")
    @ApiOperation(value = "添加轮播图")
    public Result addSlideshow(Slideshow slideshow,
                               HttpServletRequest request){
        try {
            slideshowService.addSlideshow(slideshow,request);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @PostMapping("/getSlideshowList")
    @ApiOperation(value = "后台获取轮播图列表")
    public Result getSlideshowList(@ApiParam(name = "page", value = "page 页") @RequestParam("page")Integer page,
                                   @ApiParam(name = "size", value = "size 条") @RequestParam("size")Integer size,
                                   @ApiParam(name = "shopId", value = "shopId 门店Id") @RequestParam("shopId")Integer shopId){
        try {
            PageInfo pageInfo = slideshowService.getSlideshowList(page,size,shopId);
            return new Result(true, StatusCode.OK,"操作成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @PostMapping("/delSlideshow")
    @ApiOperation(value = "删除轮播图")
    public Result delSlideshow(@ApiParam(name = "slideshowId", value = "轮播图ID") @RequestParam("slideshowId")Integer slideshowId,
                               @ApiParam(name = "picName", value = "picName 图片路径名称") @RequestParam("picName")String picName){
        try {
            slideshowService.delSlideshow(slideshowId,picName);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @PostMapping("/updateSlideshowStatus")
    @ApiOperation(value = "修改轮播图状态")
    public Result updateSlideshowStatus(@ApiParam(name = "status", value = "status 状态") @RequestParam("status")Integer status,
                                        @ApiParam(name = "ids", value = "ids 多个ID，号分割") @RequestParam("ids")String ids){
        try {
            slideshowService.updateSlideshowStatus(status,ids);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @PostMapping("/updateSlideshowFlag")
    @ApiOperation(value = "修改轮播图是否置顶")
    public Result updateSlideshowFlag(@ApiParam(name = "ids", value = "ids 多个ID，号分割") @RequestParam("ids")String ids ,
                                      @ApiParam(name = "flag", value = "flag 是否置顶 0非置顶 1置顶") @RequestParam("flag")Integer flag){
        try {
            slideshowService.updateSlideshowFlag(ids,flag);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @GetMapping("/getSlideshowLibrary")
    @ApiOperation(value = "获取轮播图库")
    public Result getSlideshowLibrary(@ApiParam(name = "shopId", value = "shopId 门店ID") @RequestParam("shopId")Integer shopId){
        try {
            List<String> picName = slideshowService.getSlideshowLibrary(shopId);
            return new Result(true, StatusCode.OK,"操作成功",picName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }


}
